package com.sqx.modules.userCertification.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.userCertification.entity.UserCertification;
import com.sqx.modules.userCertification.service.UserCertificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 实名认证-用户端
 * @author wuChang
 * @since 2024-03-22
 */
@RestController
@Slf4j
@Api(value = "实名认证-用户端", tags = {"实名认证-用户端"})
@RequestMapping("/app/userCertification/")
public class AppUserCertificationController {
    @Autowired
    private UserCertificationService userCertificationService;

    private final ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(true);

    /**
     * 新增或修改实名认证
     *@param userCertification 实名认证
     */
    @Login
    @ApiOperation("新增或修改实名认证")
    @PostMapping(value = "saveUserCertification")
    public Result saveUserCertification(@ApiParam("用户id")@RequestAttribute Long userId,
                                        @ApiParam("实名认证实体")UserCertification userCertification) {
        userCertification.setUserId(userId);
        userCertification.setStatus(0);
        reentrantReadWriteLock.writeLock().lock();
        try {
            return userCertificationService.saveUserCertification(userCertification);
        }catch (Exception e) {
            log.error("下单异常：" + e.getMessage(), e);
        } finally {
            reentrantReadWriteLock.writeLock().unlock();
        }
        return Result.error("系统繁忙，请稍后再试！");
    }

    /**
     * 获取实名认证详情
     *@param userId 用户id
     */
    @Login
    @ApiOperation("获取实名认证详情")
    @GetMapping(value = "getUserCertificationByUserId")
    public Result getUserCertificationById(@ApiParam("用户id")@RequestAttribute Long userId) {
        return Result.success().put("data", userCertificationService.getOne(new QueryWrapper<UserCertification>().eq("user_id", userId)));
    }

}